from src.application.order.uow import OrderUoW
from src.application.proxy.dto.proxies import ProxiesDTO
from src.application.proxy.dto.proxy import ProxyDTO
from src.application.proxy.enums import ProxyStatus
from src.application.proxy.use_cases.get_all_proxies import GetAllProxiesHandler
from src.application.proxy.use_cases.get_available_proxies import GetAvailableProxies, GetAvailableProxiesHandler
from src.application.proxy.use_cases.get_proxy import GetProxy, GetProxyHanadler
from src.application.proxy.use_cases.update_proxy_status import UpdateProxyStatus, UpdateProxyStatusHandler
from src.application.server.enums import ServerPrivacy, ServerType


class ProxyService:
    def __init__(
        self,
        *,
        uow: OrderUoW,
    ) -> None:
        self._uow = uow

    async def get_proxy_by_id(self, proxy_id: int) -> ProxyDTO:
        """Получаем прокси по id"""

        command = GetProxy(proxy_id)
        return await GetProxyHanadler(self._uow)(command=command)

    async def get_available_proxies(
        self,
        proxy_type: ServerType | str,
        proxy_privacy: ServerPrivacy | str,
        country_id: int,
        provider_id: int,
        category_id: int,
        tg_user_id: int,
        site_id: int | None = None,
        limit: int | None = None,
    ) -> ProxiesDTO:
        """Получаем доступные для аренды прокси"""

        if isinstance(proxy_type, str):
            proxy_type = getattr(ServerType, proxy_type)
        if isinstance(proxy_privacy, str):
            proxy_privacy = getattr(ServerPrivacy, proxy_privacy)

        query = GetAvailableProxies(
            proxy_type=proxy_type,
            proxy_privacy=proxy_privacy,
            country_id=country_id,
            provider_id=provider_id,
            category_id=category_id,
            site_id=site_id,
            limit=limit,
            tg_user_id=tg_user_id,
        )
        return await GetAvailableProxiesHandler(uow=self._uow)(query=query)

    async def get_all_proxies(self) -> ProxiesDTO:
        return await GetAllProxiesHandler(uow=self._uow)()

    async def update_proxy_status(self, proxy_id: int, status: ProxyStatus) -> None:
        """Обновляем статус прокси"""

        command = UpdateProxyStatus(proxy_id=proxy_id, status=status)
        return await UpdateProxyStatusHandler(self._uow)(command=command)
